//
//  LOLCategoryView.m
//  LOLNews
//
//  Created by Jake on 16/10/29.
//  Copyright © 2016年 KK. All rights reserved.
//

#import "LOLCategoryView.h"
#import "LOLCategoryButton.h"

@interface LOLCategoryView ()

/**
 *  按钮数组
 */
@property(nonatomic,strong) NSArray<UIButton *> *buttons;
/**
 *  当前选中的按钮
 */
@property (nonatomic,weak) UIButton *selectedButton;
/**
 *  指示条
 */
@property (nonatomic,weak) UIImageView *indicatorBar;

@end

@implementation LOLCategoryView

#pragma mark - 按钮点击事件
- (void)clickAction:(UIButton *)sender {
    
    //如果选中的按钮为当前的按钮，不再执行以下代码
    if (self.selectedButton == sender) return;
    //取消选中上一个按钮
    self.selectedButton.selected = NO;
    //选中当前按钮
    sender.selected = YES;
    //选中按钮为当前按钮
    self.selectedButton = sender;
    
    //更新滚动条的约束
    [self.indicatorBar mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.buttons[0]).offset(sender.bounds.size.width * sender.tag);
    }];

}

#pragma Mark - 设置界面
- (void)setupUI {
    
    self.backgroundColor = [UIColor whiteColor];
    
    // 标题颜色:普通状态
    UIColor *normalColor = [UIColor cz_colorWithHex:0x212121];
    // 标题颜色:选中状态
    UIColor *selectedColor = [UIColor cz_colorWithHex:0xBC985A];
    
    //按钮名字数组
    NSArray *btnNames = @[@"最新",@"活动",@"娱乐",@"官方",@"攻略",@"收藏"];
    
    //创建六个按钮
    NSMutableArray *arrM = [NSMutableArray array];
    NSInteger index = 0;
    for (NSString *name in btnNames) {
        
        UIButton *btn = [self cz_textButton:name fontSize:14 normalColor:normalColor selectedColor:selectedColor];
        [self addSubview:btn];
        
        //设置 tag
        btn.tag = index;
        index++;
        
        [arrM addObject:btn];
        
        //按钮监听
        [btn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchDown];
        
        self.buttons = arrM.copy;
    }
    
    //自动布局
    [self.buttons[0] mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.top.bottom.equalTo(self);
    }];
    
    [self.buttons[1] mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.buttons[0].mas_right);
        make.top.bottom.equalTo(self);
        make.width.equalTo(self.buttons[0]);
    }];
    
    [self.buttons[2] mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.buttons[1].mas_right);
        make.top.bottom.equalTo(self);
        make.width.equalTo(self.buttons[1]);
    }];
    
    [self.buttons[3] mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.buttons[2].mas_right);
        make.top.bottom.equalTo(self);
        make.width.equalTo(self.buttons[2]);
    }];
    
    [self.buttons[4] mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.buttons[3].mas_right);
        make.top.bottom.equalTo(self);
        make.width.equalTo(self.buttons[3]);
    }];
    
    [self.buttons[5] mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.buttons[4].mas_right);
        make.top.bottom.right.equalTo(self);
        make.width.equalTo(self.buttons[4]);
    }];
    
    //创建滚动条
    UIImage *indicatorImage = [UIImage imageNamed:@"personal_segment_selected_mark"];
    UIImageView *indicatorBar = [[UIImageView alloc] init];
    indicatorBar.image = indicatorImage;
    [self addSubview:indicatorBar];
    
    self.indicatorBar = indicatorBar;
    
    //自动布局
    [indicatorBar mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(self.buttons[0].mas_bottom).offset(-5);
        make.height.mas_equalTo(5);
        make.centerX.equalTo(self.buttons[0]);
        make.width.mas_equalTo(36);
    }];

    //选中一个按钮
    self.selectedButton = self.buttons[0];
    self.buttons[0].selected = YES;
}

#pragma mark - 创建按钮
- (LOLCategoryButton *)cz_textButton:(NSString *)title fontSize:(CGFloat)fontSize normalColor:(UIColor *)normalColor selectedColor:(UIColor *)selectedColor {
    
    LOLCategoryButton *button = [[LOLCategoryButton alloc] init];
    
    [button setTitle:title forState:UIControlStateNormal];
    
    [button setTitleColor:normalColor forState:UIControlStateNormal];
    [button setTitleColor:selectedColor forState:UIControlStateSelected];
    
    button.titleLabel.font = [UIFont systemFontOfSize:fontSize];
    
    [button sizeToFit];
    
    return button;
}

@end
